|
|
|
|
|
|
|
Handling errors that originate in your component and that your component must enforce is similar to error handling in standard Visual Basic applications. It would follow a pattern similar to Listings 18.1 and 18.2. |
|
|
|
|
|
|
|
|
When you design your component to allow a client application to handle an error, you must raise that error. That is, the method in your component that trapped the error tells the compiler to notify the previous method (of the client application) on the call stack that it must catch the error your component is raising up the stack. It's sort of like playing hot potatoes, only you're passing the hot potato up the call stack to the body of code that called your component. |
|
|
|
|
|
|
|
|
Raising an error involves the Raise method. To use the Raise method, your method should resemble the code outline in Listing 18.3. |
|
|
|
|
|
|
|
|
LISTING 18.3. THERaiseMETHOD IN ACTION. |
|
|
|
 |
|
|
|
|
1: Public Sub doSomething()
2: On Error GoTo doSomethingErr
3: 'Do some code here
4: Exit Sub
5: doSomethingErr:
6: Err.Raise _
7: Number:=(SOME_ERR_NUMBER + vbObjectError), _
8: Source:=COMPONENT_SOURCENAME, _
9: Description:=Something's wrong here
10: End Sub |
|
|
|
|
|
|
|
|
You can also use the Raise method without error handling. Listing 18.4 shows an example of using the Raise method with error handling disabled. |
|
|
|
|
|
|
|
|
LISTING 18.4. USING THERaiseMETHOD WITH ERROR HANDLING DISABLED. |
|
|
|
 |
|
|
|
|
1: Public Sub Run()
2: ' Use in - line error handling.
3: On Error Resume Next
4: ' Do something. . .
5: ' If the following test fails, raise an error in
6: ' the client that called the method.
7: ' Disable in - line error handling.
8: If SomethingIsWrong Then
9: On Error Goto 0
10: Err.Raise _
11: Number:=(SOME_ERR_NUMBER + vbObjectError), _
12: Source:=COMPONENT_SOURCENAME, _
|
|
|
|
|
|